<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use app\admin\model\RouteModel;
use app\portal\service\DesignerService;
use cmf\controller\AdminBaseController;
use app\portal\model\PortalDesignerModel;
use think\Db;
use app\admin\model\ThemeModel;


class AdminDesignerController extends AdminBaseController
{
   public function index()
   {
       $param = $this->request->param();


       $DesignerService = new DesignerService();
       $data        = $DesignerService->adminDesingerList($param);
       $data->appends($param);

       $this->assign('designers', $data->items());
       $this->assign('page', $data->render());

       return $this->fetch();

   }

   
    /**
     * 添加设计师
     * @adminMenu(
     *     'name'   => '添加设计师',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加设计师',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $themeModel        = new ThemeModel();
        $designerThemeFiles = $themeModel->getActionThemeFiles('portal/Designer/index');
        $this->assign('designer_theme_files', $designerThemeFiles);
        return $this->fetch();
    }
    /**
     * 添加设计师提交
     * @adminMenu(
     *     'name'   => '添加设计师提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加设计师提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];
//            $result = $this->validate($post, 'AdminArticle');
//            if ($result !== true) {
//                $this->error($result);
//            }

            $portalDesignerModel = new PortalDesignerModel();

            if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
                $data['post']['more']['photos'] = [];
                foreach ($data['photo_urls'] as $key => $url) {
                    $photoUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
                }
            }

            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }
            $portalDesignerModel->adminAddDesigner($data['post']);

            $data['post']['id'] = $portalDesignerModel->id;

            $this->success('添加成功!', url('AdminDesigner/edit', ['id' => $portalDesignerModel->id]));
        }

    }
    /**
     * 编辑设计师
     * @adminMenu(
     *     'name'   => '编辑设计师',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑设计师',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');

        $portalDesignerModel = new portalDesignerModel();
        $post            = $portalDesignerModel->where('id', $id)->find();

        $themeModel        = new ThemeModel();
        $designerThemeFiles = $themeModel->getActionThemeFiles('portal/Designer/index');
        $routeModel         = new RouteModel();
        $alias              = $routeModel->getUrl('portal/Designer/index', ['id' => $id]);
        $post['post_alias'] = $alias;
        $this->assign('designer_theme_files', $designerThemeFiles);
        $this->assign('post', $post);

        return $this->fetch();
    }

    /**
     * 编辑设计师提交
     * @adminMenu(
     *     'name'   => '编辑设计师提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑设计师提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data = $this->request->param();

//        $result = $this->validate($data['post'], 'AdminPage');
//        if ($result !== true) {
//            $this->error($result);
//        }

        $DesignerModel = new PortalDesignerModel();

        $DesignerModel->adminEditDesigner($data['post']);

        $this->success('保存成功!');
    }


    /**
     * 删除设计师
     * @author    iyting@foxmail.com
     * @adminMenu(
     *     'name'   => '删除设计师',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除设计师',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $param           = $this->request->param();
        $portalDesignerModel = new PortalDesignerModel();

        if (isset($param['id'])) {
            $id           = $this->request->param('id', 0, 'intval');
            $result       = $portalDesignerModel->where(['id' => $id])->find();
            $data         = [
                'object_id'   => $result['id'],
                'create_time' => time(),
                'table_name'  => 'portal_designer',
                'name'        => $result['post_title'],
                'user_id'=>cmf_get_current_admin_id()
            ];
            $resultPortal = $portalDesignerModel
                ->where(['id' => $id])
                ->update(['delete_time' => time()]);
            if ($resultPortal) {
                Db::name('recycleBin')->insert($data);
            }
            $this->success("删除成功！", '');

        }

        if (isset($param['ids'])) {
            $ids     = $this->request->param('ids/a');
            $recycle = $portalDesignerModel->where(['id' => ['in', $ids]])->select();
            $result  = $portalDesignerModel->where(['id' => ['in', $ids]])->update(['delete_time' => time()]);
            if ($result) {
                foreach ($recycle as $value) {
                    $data = [
                        'object_id'   => $value['id'],
                        'create_time' => time(),
                        'table_name'  => 'portal_designer',
                        'name'        => $value['post_title'],
                        'user_id'=>cmf_get_current_admin_id()
                    ];
                    Db::name('recycleBin')->insert($data);
                }
                $this->success("删除成功！", '');
            }
        }
    }
    
    

}
